package com.ruoyi.project.system.device.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.device.domain.*;
import com.ruoyi.project.system.device.mapper.MyDeviceDetailsMapper;
import com.ruoyi.project.system.device.mapper.MyPlanMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.device.mapper.MyDeviceMapper;
import com.ruoyi.project.system.device.service.IMyDeviceService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-30
 */
@Service
public class MyDeviceServiceImpl implements IMyDeviceService
{
    @Autowired
    private MyDeviceMapper myDeviceMapper;

    @Autowired
    private MyDeviceDetailsMapper myDeviceDetailsMapper;

    @Autowired
    private MyPlanMapper myPlanMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public MyDevice selectMyDeviceById(Long id)
    {
        return myDeviceMapper.selectMyDeviceById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param myDevice 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<MyDevice> selectMyDeviceList(MyDevice myDevice)
    {
        return myDeviceMapper.selectMyDeviceList(myDevice);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param myDevice 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertMyDevice(MyDevice myDevice)
    {
        /*MyDevice myDevice = new MyDevice();
        BeanUtils.copyProperties(myDeviceDTO,myDevice);*/
        myDevice.setCreateTime(DateUtils.getNowDate());
        int result = myDeviceMapper.insertMyDevice(myDevice);
        myDevice.getDeviceDetails().setCreateTime(DateUtils.getNowDate());
        myDevice.getDeviceDetails().setDeviceId(myDevice.getId());
        myDeviceDetailsMapper.insertMyDeviceDetails(myDevice.getDeviceDetails());

        return result;

    }

    /**
     * 修改【请填写功能名称】
     *
     * @param myDevice 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateMyDevice(MyDevice myDevice)
    {
        /*myDevice.setUpdateTime(DateUtils.getNowDate());
        return myDeviceMapper.updateMyDevice(myDevice);*/
        /*MyDevice myDevice = new MyDevice();
        BeanUtils.copyProperties(myDeviceDTO,myDevice);*/
        myDevice.setUpdateTime(DateUtils.getNowDate());
        int result = myDeviceMapper.updateMyDevice(myDevice);
        myDevice.getDeviceDetails().setUpdateTime(DateUtils.getNowDate());
        myDeviceDetailsMapper.updateMyDeviceDetails(myDevice.getDeviceDetails());

        return result;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMyDeviceByIds(String ids)
    {
        return myDeviceMapper.deleteMyDevice(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMyDeviceById(Long id)
    {
        return myDeviceMapper.deleteMyDeviceById(id);
    }

    @Override
    public MyDeviceDTO queryDetail(Long planId) {
        MyDevice myDevice = myDeviceMapper.queryOne();
        MyDeviceDTO deviceDTO = new MyDeviceDTO();
        BeanUtils.copyProperties(myDevice,deviceDTO);
        /*MyDeviceDetails queryDetail = MyDeviceDetails.builder().deviceId(myDevice.getId()).build();
        if(planId != null){
            queryDetail.setId(detailId);
        }*/
        if(planId == null || planId == 0){
            planId = null;
        }
        MyDeviceDetails details = myDeviceDetailsMapper.selectMyDeviceDetailsByPlanId(planId,myDevice.getId());

          if(Objects.nonNull(details)){
            List<SettingDTO> settingDTOList = Lists.newArrayList();

            String[] setting1 = details.getCangtiT1Seting().split(",");
            String[] setting2 = details.getCangtiC2Seting().split(",");
            for (int i = 0; i < setting1.length; i++) {
                SettingDTO settingDTO = SettingDTO.builder().cangtiT1Seting(setting1[i]).cangtiC2Seting(setting2[i]).build();
                settingDTOList.add(settingDTO);
            }
            /*deviceDetails.forEach(x->{
                MyDeviceDtailsDTO deviceDtailsDTO = new MyDeviceDtailsDTO();
                BeanUtils.copyProperties(x,deviceDtailsDTO);
                detailsList.add(deviceDtailsDTO);
                if(){

                }
                SettingDTO settingDTO = SettingDTO.builder().cangtiT1Seting(x.getCangtiT1Seting()).cangtiC2Seting(x.getCangtiC2Seting()).build();
                settingDTOList.add(settingDTO);
            });*/
            MyDeviceDtailsDTO deviceDtailsDTO = new MyDeviceDtailsDTO();
            BeanUtils.copyProperties(details,deviceDtailsDTO);
            //deviceDTO.setDetailsList(detailsList);
            deviceDTO.setSettingDTOS(settingDTOList);
            deviceDTO.setDtailsDTO(deviceDtailsDTO);
        }else {
            deviceDTO.setDetailsList(Lists.newArrayList());
            deviceDTO.setSettingDTOS(Lists.newArrayList());
            deviceDTO.setDtailsDTO(new MyDeviceDtailsDTO());
        }
          deviceDTO.setPlanId(details.getPlanId());
        deviceDTO.setMyPlans(myPlanMapper.selectList());
        return deviceDTO;
    }

    @Override
    public Long saveDetail(MyDeviceDtailsDTO dtailsDTO,List<SettingDTO> settingDTOS,Long planId) {
        MyDeviceDetails deviceDetails = new MyDeviceDetails();
        BeanUtils.copyProperties(dtailsDTO,deviceDetails);
        if(CollectionUtils.isNotEmpty(settingDTOS)){
            List<String> cangtiT1Setings = Lists.newArrayList();
            List<String> cangtiC2Setings = Lists.newArrayList();
            settingDTOS.forEach(x->{
                cangtiT1Setings.add(x.getCangtiT1Seting());
                cangtiC2Setings.add(x.getCangtiC2Seting());
            });
            deviceDetails.setCangtiT1Seting(StringUtils.join(cangtiT1Setings,","));
            deviceDetails.setCangtiC2Seting(StringUtils.join(cangtiC2Setings,","));
        }
        if(planId != null){
            MyPlan myPlan =  myPlanMapper.selectMyPlanById(planId);
            deviceDetails.setPlanId(myPlan.getId());
            deviceDetails.setId(myPlan.getDeviceDetailsId());
            myDeviceDetailsMapper.updateMyDeviceDetails(deviceDetails);
            Integer index = myPlanMapper.selectList().size()+1;
            myPlan.setName(dtailsDTO.getPlanName());
            myPlan.setSampleName(dtailsDTO.getSampleName());
            if(myPlan.getName() == null || myPlan.getName().equals("")){
                myPlan.setName(myPlan.getName()==null?"方案"+index:myPlan.getName());
            }
            myPlanMapper.updateByPrimaryKey(myPlan);
        }else{
            myDeviceDetailsMapper.insertMyDeviceDetails(deviceDetails);
            Integer index = myPlanMapper.selectList().size()+1;
            MyPlan myPlan = MyPlan.builder().deviceId(deviceDetails.getDeviceId()).deviceDetailsId(deviceDetails.getId()).build();
            myPlan.setName(dtailsDTO.getPlanName());
            myPlan.setSampleName(dtailsDTO.getSampleName());
            if(myPlan.getName() == null || myPlan.getName().equals("")){
                myPlan.setName(myPlan.getName()==null?"方案"+index:myPlan.getName());
            }
            myPlanMapper.insert(myPlan);
            planId = myPlan.getId();
        }
        return planId;
    }

    @Override
    public List<MyPlan> queryMyPlans() {
        return myPlanMapper.selectList();
    }
}
